package decision_tree;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import decision_tree.dto.Sample;
import decision_tree.metric.EntropyManager;
import decision_tree.question.Question;

public class QuestionAnalyzer {
	protected EntropyManager ep;
	public QuestionAnalyzer(EntropyManager _ep) {
		ep = _ep;
	}
	
	public Question findBestQuestion(List<Sample> sampleList, List<Question> questionList, String filename) {
		
		List<Double> miList = getMutualInformationyList(sampleList, questionList);
		
		Iterator<Double> miItr = miList.iterator();
		Iterator<Question> qItr = questionList.iterator();
		
		Question bestQuestion = null;
		double bestValue = 0;
		while(qItr.hasNext()) {
			Question question = qItr.next();
			double value = miItr.next();
			if(bestQuestion == null || value > bestValue) {
				bestQuestion = question;
				bestValue = value;
			}
		}
		return bestQuestion;
	}

	public List<Double> getMutualInformationyList (List<Sample> sampleList, List<Question> questionList) {
		List<Double> ceList = getConditionalEntropyList(sampleList, questionList);
		double entropy = ep.getEntropy(sampleList);
		List<Double> resultList = new ArrayList<Double>(ceList.size());
		
		for(double ce : ceList) {
			resultList.add(entropy - ce);
		}
		
		return resultList;
	}

	public List<Double> getConditionalEntropyList (List<Sample> sampleList, List<Question> questionList) {
		List<Double> resultList = new ArrayList<Double>(questionList.size());
	
		for(Question question : questionList) {
			double value = ep.getConditionalEntropy(sampleList, question);
			resultList.add(value);
		}
		return resultList;
	}
}
